package com.example.bootcurriculumproject.controller;

import com.example.bootcurriculumproject.dto.ShipDto;
import com.example.bootcurriculumproject.entity.Goods;
import com.example.bootcurriculumproject.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //增加一个商品
    @PostMapping("one")
    @ResponseBody
    @CrossOrigin
    public int addOne(@RequestBody Goods goods){
       return goodsService.addOne(goods);
    }

    //修改商品信息（id值是不可以更改的，前端设计时id值直接查询显示默认不修改）
    @PutMapping
    @ResponseBody
    @CrossOrigin
    public boolean update(@RequestBody Goods goods){
      return goodsService.update(goods);
    }

    //删除商品信息
    @DeleteMapping()
    @ResponseBody
    @CrossOrigin
    public boolean Delete(@RequestParam String no){
        log.info("-------"+no);
        return goodsService.delete(no);
    }
    //搜索框查询
    @GetMapping("/getName")
    @ResponseBody
    @CrossOrigin
    public List findByName(@RequestParam String name){//http://localhost:8080/goods/getName?name=ddyt
        log.info("name:::::"+name);
        return goodsService.findByName(name);
    }
    //商品数据全部显示
    @GetMapping("/getAll")
    @ResponseBody
    @CrossOrigin
    public List findByAll(){
        return goodsService.findByAll();
    }

    //分类商品
    @GetMapping("/type/{type}")
    @ResponseBody
    @CrossOrigin
    public List findGoodsByType(@PathVariable("type")String type){
        //http://localhost:8080/goods/type/fngm
        return goodsService.findGoodsByType(type);
    }
    //单件商品直接提交订单
    @PostMapping("/submitOne")
    @ResponseBody
    @CrossOrigin
    public boolean submitOneGoods(@RequestBody ShipDto shipDto){
        goodsService.submitOneGoods(shipDto);
        return true;
    }
    //根据id查询商品
    @GetMapping("/getById")
    @ResponseBody
    @CrossOrigin

//    public Goods getGoodsById(@PathVariable("goodId") String goodId){
//        //http://localhost:8080/goods/getById/AR880948awgl
//        Goods goods = goodsService.getGoodsById(goodId);
//        return goods;
//    }
    public Goods getGoodsById(@RequestParam String goodId){
        //http://localhost:8080/goods/getById?goodId=AR880948awgl
        Goods goods = goodsService.getGoodsById(goodId);
        return goods;
    }
}
